var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
     id:'',
     imgsrc:'',
     isbuy:'',
     title:'',
     nums:'',
     studyNum:'',
     price:'',
     buyCourse:'',
     freshSection:'',
   leftStyle:'margin-left:126rpx; width:117rpx;',
  rightStyle:'margin-right:126rpx; width:117rpx;',
    teacherAvatar:'',
    teacherName:'',
    teacherDes:'',
    dis:true,
    course_content:'1、单词讲解：经典诠释每个单词发音、词性、用法及记忆方法。\n\n 2、单词练习：有效纠正单词发音问题。\n\n 3、课文讲解：名师详细讲解全册课程，绝不遗漏任何细节；帮助你彻底吃透学懂经典教材。\n\n 4、语法讲解：名师详细讲解重点语法知识，条分缕析，生动有趣，语法学习不再枯燥。',
    courseArray:[
      {
        title:'一、新概念2册课'
       
      },
      {
        title: '三、新概念2册课'
       
      },
      ],
      chaptersArray:[]
  },
   toNews:function(res)
   {
     var id=res.detail.id;
     switch (id){
       case 'online':
        this.setData({
          dis:true
        })
       // console.log(true)
        break
        case 'near':
        this.setData({
          dis:false
        })
        //console.log(false)
     }
   },
   toVideo:function(event){
     console.log(event)
     console.log(111)
     console.log(this.data.isbuy)
     if(this.data.isbuy){
       console.log(event)
       var id = event.currentTarget.dataset.id
       wx.navigateTo({
         url: '../video/video?id=' + id,
       })
      
     }else{
      
       this.buyLesson(event)
     }
    
   },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    var that=this
    var id=options.id
    this.setData({
       id:id
    })
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    wx.request({
      url: app.globalData.baseUrl + 'coursecenter/course/' + that.data.id,
      method: 'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)

        that.setData({
          imgsrc: res.data.data.img,
          title: res.data.data.title,
          price:res.data.data.price,
          freshSection: res.data.data.sectionCurrent,
          nums: res.data.data.sectionTotal,
          studyNum: res.data.data.studyNum,
          teacherAvatar: res.data.data.teacherAvatarUrl,
          teacherName: res.data.data.teacherName,
          teacherDes: res.data.data.teacherDescription,
          isbuy:res.data.data.isBought,
          course_content: res.data.data.descriptionDetail
        })
        if(that.data.isbuy){
          that.setData({
            buyCourse: 'opacity: 0.5;'
          })
          console.log('buycourse')
          console.log(that.data.buyCourse)
        }
      
        // var line = res.data.data.courseOutlineList
        // try{
        //   for (var i = 0; i < line.length;i++){
            
        //      that.setData({
        //        [title]:line[i].title
        //      })
        //    }
        // }catch(err){
        //   console.log(err)
        // }
        var detail=res.data.data.courseDetailList
        try{
          console.log(detail[0].title)
          for(var i=0;i<detail.length;i++){
            var title ='chaptersArray['+i+'].title'
            var num = 'chaptersArray[' + i +'].nums'
            var time ='chaptersArray['+i+'].times'
            var comment='chaptersArray['+i+'].comments'
            var coverImg = 'chaptersArray[' + i + '].coverImg'     
            var id='chaptersArray['+i+'].id'
            that.setData({
              [title]:detail[i].title,
              [num]:detail[i].title.substring(0,3),
              [id]:detail[i].id,
              [time]:detail[i].studyNum,
              [comment]:detail[i].commentTotal,
              [coverImg]:detail[i].imgUrl
            })
          }
          console.log(that.data.chaptersArray[0].title)
        }catch(err){
          console.log(err)
        }
          
        }
        
    })
  },
  buyLesson:function(event){
    console.log('buy lesson')
    var integral=wx.getStorageSync('integral')
    var price=this.data.price
    var that=this
    if(this.data.isbuy){
      console.log('buyed')
    }else{
      if (integral >= price) {
        wx.showModal({
          title: '',
          content: '是否愿意花' + price + '积分来购买视频',
          success:function(res){
            if(res.confirm){
              wx.request({
                url: app.globalData.baseUrl+'coursecenter/course/'+that.data.id,
                method:'post',
                header:{
                  'cookie':'JSESSIONID='+wx.getStorageSync('sessionID'),
                   'content-type': 'application/x-www-form-urlencoded'
                },
                success:function(res){
                  console.log(res)
                  if(res.data.msg=='请求成功'){
                    that.onShow()
                    wx.showToast({
                      title: '购买成功',
                    })
                  }else{
                    wx.showModal({
                      title: '',
                      content: '积分不足，是否充值积分？',
                      success:function(res){
                        if(res.confirm){
                          wx.navigateTo({
                            url: '/geren/yuePage/yuePager',
                          })
                        }
                      }
                    })
                  }
                },
                fail:function(err){
                  wx.showToast({
                    title: '请求失败',
                  })
                }
              })
            }else{
              // wx.showModal({
              //   title: '',
              //   content: '视频所需积分为'+price+'您的积分为'+integral+'积分不足',
              // })
            }
          }
        })
      }
    }
   
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})